<?php

	include_once '../config.inc.php';
	include_once '../clases/menu_element.php';
	include_once '../controladores/ControladorElementos.php';
	include_once '../controladores/ControladorSecciones.php';
	
			$usr = $_GET['usr'];
			$app = $_GET['app'];
			$secc = $_GET['sec'];
			$elm = $_GET['elm'];

      //Actualizando los elementos actuales:
	  
	  isset($_POST['id']) ? $updatingIds = $_POST['id'] : $updatingIds = 0;
	  
	  if ($updatingIds != 0) {

			$updatingIds = $_POST['id'];
			$updatingOptions = $_POST['currentOptionName'];
			$updatingActions = $_POST['currentActionOptions'];
			$updatingSections = $_POST['currentSectionsPath'];
			
			$updateElements = array();
			$i = 0;
			
			for ($i = 0; $i < count($updatingIds); $i++)
				if ($updatingActions[$i] == "1")	
					$updateElements[$i] = new MenuElement($updatingIds[$i],"","",$updatingOptions[$i],$updatingSections[$i]);
			    else
					$updateElements[$i] = new MenuElement($updatingIds[$i],"","",$updatingOptions[$i],"");
					
			$command = new ControladorElementos(MENU_ELEMENT);
			
			for ($j=0; $j < count($updateElements); $j++)							
			$command->updateElement(array("element_id" => $updateElements[$j]->getSecId(), "me_content" => $updateElements[$j]->getContent(), "me_link_to" => $updateElements[$j]->getLinkTo()));
			
	  }
		//Creando los elementos nuevos de existir.
		
		isset($_POST['optionName']) ? $options = $_POST['optionName'] : $options = 0;
		
			if ($options != 0){
					
					$options = $_POST['optionName'];
					$actions = $_POST['actionOptions'];
					$sections = $_POST['sectionsPath'];
					
				   $command = new ControladorElementos(); 	
			       $menuListId = $command->obtainIdMenuList($app,$secc);
				   
					$dynamicIndex = null;
					$elements = array();
					
				for ($i=0; $i < count($actions); $i++)
				  if ($actions[$i] == "1")	
					$elements[$i] = new MenuElement($secc,$menuListId,($i+1),$options[$i],$sections[$i]);
				  else
					$elements[$i] = new MenuElement($secc,$menuListId,($i+1),$options[$i],"");
									
					$command = new ControladorElementos(MENU_ELEMENT);
					
						for ($j=0; $j < count($elements); $j++)
							$command->createElement(array("sec_id" => $elements[$j]->getSecId(), "men_element_id" => $elements[$j]->getMenuElementId(), "element_order" => $elements[$j]->getElementOrder(), "me_content" => $elements[$j]->getContent(), "me_link_to" => $elements[$j]->getLinkTo()));
							
			}
			
		header("Location:section_elements.php?usr=".$usr."&app=".$app."&sec=".$secc."");	
?>